//index.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../utils/user.js');
import chartPie from '../../components/chartPie/chartPie.js'
var app = getApp();   
let config = {
  data: {
    canvasId: 'chart_pie',
    data: {},
    current: 0,
    bannerList: [
      
    ],
    articleList: [],
    userInfo: {},
    hasMobile: '',
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.getIndexData();
    this.articleList();
  },
  onShow: function () {
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('access_token');
    // 页面显示
    if (userInfo && token) {
      app.globalData.userInfo = userInfo;
      app.globalData.token = token;
    }
    this.setData({
      userInfo: app.globalData.userInfo,
    });
  },
  getIndexData: function () {
    let that = this;
    var data = new Object();
    util.appGet(api.IndexUrlBanner).then(function (res) {
      if (res.code === 0) {
        data.bannerList = res.data.bannerList
        that.setData(data);
      }
    });
  },
 
  // onShow() {
  //   let chartP = chartPie({
  //     canvasId: this.data.canvasId,
  //     width: 630,
  //     height: 300
  //   })


  //   var data = {
  //     token: wx.getStorageSync('token'),
  //   };
  //   // return;
  //   wx.request({
  //     url: "https://government.chaolian123.com/NewIndex",
  //     data: data,
  //     method: 'POST',
  //     success: res => {
  //       console.log(res)
  //       if (res.data.code == 1) {
  //         this.setData({
  //           data: res.data.List
  //         })
  //         console.log(this.data.data)
  //       } else {
  //         wx.showToast({
  //           title: res.data.msg,
  //           icon: 'none'
  //         })
  //       }
  //     },
  //     fail: err => {
  //       console.log(111, res);
  //       defVal.fail(err.message)
  //       wx.showModal({
  //         title: '警告',
  //         content: err.message
  //       })
  //     }
  //   })
  // },
  articleList() {
    util.request(api.IndexUrlArticle,{}, 'GET').then( res => {
      console.log(res);
      this.setData({
        articleList: res.data.articleList
      })
    })
  },
  goDetial(event) {
    let id = event.currentTarget.dataset.id;
    if (wx.getStorageSync('token') == undefined) {
      wx.showToast({
        title: '你还未登录！',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/pages/new_info/new_info?id=' + id
      })
    }
  },

  goReportWay() {
    wx.navigateTo({
      url: '/pages/report/report_way/report_way'
    })
  },

  gophone() {
    wx.makePhoneCall({
      phoneNumber: '4001066209'
    });
  },

  goProjectMessage(event) {
    wx.navigateTo({
      url: '/pages/project/project_message/project_message'
    })
  },


  changeFun(e) {
    this.setData({ current: e.detail.current })
  },
  getIndex(e) {
    this.setData({ current_text: e.detail.index })
  },

 
  bindGetUserInfo(e) {
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');
    if (userInfo && token) {
      return;
    }
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      user.loginByWeixin(e.detail).then(res => {
        this.setData({
          userInfo: res.data.userInfo
        });
        app.globalData.userInfo = res.data.userInfo;
        app.globalData.token = res.data.token;

      }).catch((err) => {
        console.log(err)
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告通知',
        content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                  user.loginByWeixin(e.detail).then(res => {
                    this.setData({
                      userInfo: res.data.userInfo
                    });
                    app.globalData.userInfo = res.data.userInfo;
                    app.globalData.token = res.data.token;
                  }).catch((err) => {
                    console.log(err)
                  });
                }
              }
            })
          }
        }
      });
    }
  },
  exitLogin: function () {
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function (res) {
        if (res.confirm) {
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.switchTab({
            url: '/pages/index/index'
          });
        }
      }
    })
  }





}
Page(
  config
)